package giit.computerbase.server;

import giit.computerbase.dao.MySQL;
import giit.computerbase.dao.SQL;
import giit.computerbase.util.Application;
import giit.computerbase.util.Hash;

/**
 * User: tum0r
 * Package Name: giit.computerbase.server
 * Time: 2018-11-29 Thursday 18:42:48
 */
public class ChangePasswordServer {
    private SQL sql;
    private Hash hash;

    public ChangePasswordServer() {
        try {
            sql = new MySQL();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        sql.open((String) Application.getConfig().databaseConfig.get("address"),
                (int) Application.getConfig().databaseConfig.get("port"),
                Application.DATABASE_NAME,
                (String) Application.getConfig().databaseConfig.get("encode"),
                (String) Application.getConfig().databaseConfig.get("user"),
                (String) Application.getConfig().databaseConfig.get("password"));
        hash = new Hash();
    }

    public boolean judgeUser(String username) {
        return sql.query("select * from user where name=?", new Object[]{username}).size() != 0;
    }

    public boolean change(String username, String password) {
        return sql.update("update user set password=? where name=?", new Object[]{hash.encryption(hash.encryption(password, Hash.SHA256), Hash.MD5), username});
    }
}
